<extend name="./Base/base" />

<block name="main">
	<div class="row" style="margin-left:300px; ">
		<div class="col-sm-6">	
			<form id="form_ft" method='post' name='myform' enctype="multipart/form-data">	
				<input type="hidden"  name="id"  value="{$list.id}" />
				<div class="panel">
					<div class="panel-heading bk-bg-primary">
						<h6><i class="fa fa-indent red"></i>管理员修改</h6>							
					</div>
					<div class="panel-body">
						<div class="form-group has-success">
							<label class="control-label" for="inputSuccess1">管理员名称</label>
							<input type="text" class="form-control" value="{$list['username']}" name="username" id="inputSuccess1">
						</div>
						
						<div class="form-group has-error">
							<label class="control-label"   for="inputError1">邮箱</label>
							<input type="email" name="email" value="{$list['email']}" class="form-control" id="inputError1" >
						</div>
						<div class="form-group">
							<label for="inputfile">上传头像</label>
							<!-- <input type="file" id="inputfile" name="img" style="position:absolute;display:none;z-index:999;"> -->
							<input type='file' id='iptfileupload' onchange='show()' name="imgs"  />
							<p class="help-block">这里是图片浏览</p>
							<!-- <img src="__PUBLIC__/admin/assets/img/{$list.img}" alt="" style="width:120px;higth:120px;"> -->
							<img src='__PUBLIC__/admin/assets/img/user_code/{$list.img}' style="width:120px;higth:120px;" alt='' id='img' />
						</div>
						<!-- <input type="submit" id="login_user" class="btn btn-inverse" value="提交"> -->
						<div id="message" style="margin-left:115px;"></div>
					</div>
					<!--  <button  id="login" class="btn btn-success">提交</button> -->
					
					<button onclick="form_tf();return false;" class="btn btn-large btn-primary" style="margin-left:35%;">&nbsp&nbsp&nbsp&nbsp&nbsp提交&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</button>
				</div>
			</form>	
		</div>
	</div>
</block>


<block name="script_user">
	<script type="text/javascript">
    function getPath(obj,fileQuery,transImg) 
    {
 
		   var imgSrc = '', imgArr = [], strSrc = '' ;
		 
		   if(window.navigator.userAgent.indexOf("MSIE")>=1){ // IE浏览器判断
		    if(obj.select){
		     obj.select();
		     var path=document.selection.createRange().text;
		     alert(path) ;
		     obj.removeAttribute("src");
		     imgSrc = fileQuery.value ;
		     imgArr = imgSrc.split('.') ;
		     strSrc = imgArr[imgArr.length - 1].toLowerCase() ;
		     if(strSrc.localeCompare('jpg') === 0 || strSrc.localeCompare('jpeg') === 0 || strSrc.localeCompare('gif') === 0 || strSrc.localeCompare('png') === 0){
		      obj.setAttribute("src",transImg);
		      obj.style.filter=
		       "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+path+"', sizingMethod='scale');"; // IE通过滤镜的方式实现图片显示
		     }else{
		      //try{
		      throw new Error('File type Error! please image file upload..'); 
		      //}catch(e){
		      // alert('name: ' + e.name + 'message: ' + e.message) ;
		      //}
		     }
		    }else{
		     // alert(fileQuery.value) ;
		     imgSrc = fileQuery.value ;
		     imgArr = imgSrc.split('.') ;
		     strSrc = imgArr[imgArr.length - 1].toLowerCase() ;
		     if(strSrc.localeCompare('jpg') === 0 || strSrc.localeCompare('jpeg') === 0 || strSrc.localeCompare('gif') === 0 || strSrc.localeCompare('png') === 0){
		      obj.src = fileQuery.value ;
		     }else{
		      //try{
		      throw new Error('File type Error! please image file upload..') ;
		      //}catch(e){
		      // alert('name: ' + e.name + 'message: ' + e.message) ;
		      //}
		     }
		 
		    }
		 
		   } else{
		    var file =fileQuery.files[0];
		    var reader = new FileReader();
		    reader.onload = function(e){
		 
		     imgSrc = fileQuery.value ;
		     imgArr = imgSrc.split('.') ;
		     strSrc = imgArr[imgArr.length - 1].toLowerCase() ;
		     if(strSrc.localeCompare('jpg') === 0 || strSrc.localeCompare('jpeg') === 0 || strSrc.localeCompare('gif') === 0 || strSrc.localeCompare('png') === 0){
		      obj.setAttribute("src", e.target.result) ;
		     }else{
		      //try{
		      throw new Error('File type Error! please image file upload..') ;
		      //}catch(e){
		      // alert('name: ' + e.name + 'message: ' + e.message) ;
		      //}
		     }
		 
		     // alert(e.target.result); 
		    }
		    reader.readAsDataURL(file);
		   }
	} 
    function show()
    {
	   //以下即为完整客户端路径
	   var file_img=document.getElementById("img"),
	    iptfileupload = document.getElementById('iptfileupload') ;
	   getPath(file_img,iptfileupload,file_img) ;
    }

  function form_tf(){
  	var username = $("input[name='username']").val();
  	var email	  = $("input[name='email']").val();
  	var imgs 	  = $("input[name='imgs']").val();
  	if ($('#inputSuccess1').val() == '' | $('#inputError1').val() == '') {
  	 		$("#message").html('<p class="error" style="color:red">ERROR: 修改不能有空值</p>');
         	return false;
  	};
  	var form = new FormData(document.getElementById("form_ft"));
  	$.ajax({
  		url:"{:U('User/user_save')}",
  		type:"post",	
  		processData:false,
        contentType:false,
  		data:form,
  		success:function(data){
  			if (data == 1) {
  				alert('post异常错误');
  				return false;
  			}else if (data == 2) {
  				$("#message").html('<p class="error" style="color:red">图片上传错误</p>');
  				return false;
  			}else if (data == 3) {
  				$("#message").html('<p class="error" style="color:red">不能改成已经存在的用户名</p>');
  				return false;
  			}else {
  				var res = data.substr(0,1);
  				var	url = data.substring(2);
  				if (res == 4) {
  					window.location.href = url;
  				}else if (res == 5) {
  					alert('修改失败,请重新修改');
                	window.location.href = url;
  				};
  			};
  		}
  	});
  	
  }
 </script>
</block>